#ifndef _AVI_HEAD_INFO_
#define _AVI_HEAD_INFO_
#include "crossplat_def.h"

typedef struct
{
	off_t key;
	off_t pos;
	off_t len;
} video_index_entry;

typedef struct
{
	off_t pos;
	off_t len;
	off_t tot;
} audio_index_entry;

typedef struct track_s
{

	long   a_fmt;             /* Audio format, see #defines below */
	long   a_chans;           /* Audio channels, 0 for no audio */
	long   a_rate;            /* Rate in Hz */
	long   a_bits;            /* bits per audio sample */
	long   mp3rate;           /* mp3 bitrate kbs*/

	long   audio_strn;        /* Audio stream number */
	off_t  audio_bytes;       /* Total number of bytes of audio data */
	long   audio_chunks;      /* Chunks of audio data in the file */

	char   audio_tag[4];      /* Tag of audio data */
	long   audio_posc;        /* Audio position: chunk */
	long   audio_posb;        /* Audio position: byte within chunk */

	off_t a_codech_off;       /* absolut offset of audio codec information */ 
	off_t a_codecf_off;       /* absolut offset of audio codec information */ 

	audio_index_entry *audio_index;

} track_t;

typedef struct
{
	uint32_t  bi_size;
	uint32_t  bi_width;
	uint32_t  bi_height;
	uint16_t  bi_planes;
	uint16_t  bi_bit_count;
	uint32_t  bi_compression;
	uint32_t  bi_size_image;
	uint32_t  bi_x_pels_per_meter;
	uint32_t  bi_y_pels_per_meter;
	uint32_t  bi_clr_used;
	uint32_t  bi_clr_important;
} BITMAPINFOHEADER_avilib;

typedef struct
{
	uint16_t  w_format_tag;
	uint16_t  n_channels;
	uint32_t  n_samples_per_sec;
	uint32_t  n_avg_bytes_per_sec;
	uint16_t  n_block_align;
	uint16_t  w_bits_per_sample;
	uint16_t  cb_size;
} WAVEFORMATEX_avilib;

typedef struct _aviolde_data_
{
	uint32_t chunkId;
	uint32_t flags;
	uint32_t offset;
	uint32_t size;
}AVI_IDX_KEY;

typedef struct _avioldindex_
{
	uint32_t   fcc;
	uint32_t   date_size;
	AVI_IDX_KEY  aviIdx[0];
}AVIOLD_INDEX;

typedef struct _RIFF_HEAD_
{
	uint32_t   fccType;
	uint32_t   size;
	uint32_t   ckID;
}RIFF_HEAD;

typedef struct _MainAVIHeader_
{ 
	uint32_t  fcc;     //avih
	uint32_t  cb;
	uint32_t dwMicroSecPerFrame ; //显示每桢所需的时间ns，定义avi的显示速率 
	uint32_t dwMaxBytesPerSec; // 最大的数据传输率 
	uint32_t dwPaddingGranularity; //记录块的长度需为此值的倍数，通常是2048 
	uint32_t dwFlages; //AVI文件的特殊属性，如是否包含索引块，音视频数据是否交叉存储 
	uint32_t dwTotalFrame; //文件中的总桢数 
	uint32_t dwInitialFrames; //说明在开始播放前需要多少桢 
	uint32_t dwStreams; //文件中包含的数据流种类 
	uint32_t dwSuggestedBufferSize; //建议使用的缓冲区的大小， //通常为存储一桢图像以及同步声音所需要的数据之和 
	uint32_t dwWidth; //图像宽 
	uint32_t dwHeight; //图像高 
	uint32_t dwReserved[4]; //保留值 
}Main_AVIHeader;

#define  AUDIOFORMAT_LEN  512
#define  AVIHEADLEN       2000

typedef struct _avimainstrl{
	uint32_t		fcc;  //strl
	Main_AVIHeader   avih;
}AVIMAINSTRL;

//strh////////////////////
typedef struct _avistreamheader
{
	uint32_t  fcc;
	uint32_t  cb;   //56 0x38

	uint32_t  fccType;
	uint32_t  fccHandler;

	uint32_t  dwFlags;
	uint16_t   wPriority;
	uint16_t   wLanguage;

	uint32_t  dwInitialFrames;
	uint32_t  dwScale;
	uint32_t  dwRate;
	uint32_t  dwStart;
	uint32_t  dwLength;
	uint32_t  dwSuggestedBufferSize;
	uint32_t  dwQuality;
	uint32_t  dwSampleSize;

	struct{
		short int left;
		short int top;
		short int right;
		short int bottom;
	}rcFrame;  

}AVISTREAMHEADER;

//strf////////////////////
typedef struct _avistreamformat{
	uint32_t  fcc;
	uint32_t  cb;
	char*  pbformat;
}AVISTREAMFORMAT;

typedef struct  STREAML{
	uint32_t			fcc;  //strl
	AVISTREAMHEADER     strh; //strh
	AVISTREAMFORMAT     strf;
}STREAML;

typedef struct _avlist{
	uint32_t			fcc;
	uint32_t			cb;
	STREAML         strl;
}AVLIST; 


typedef struct  AVILIST{
	uint32_t		fcc;  //LIST
	uint32_t		cb;   //
	AVIMAINSTRL     strl;
	//	STREAMLIST  slist[]; 
}AVILIST;

typedef enum eStreamType{
	MEDIANULL = 0,
	VIDEO = 1,
	AUDIO = 2,
	AVMEDIA = 3
}ESTREAMTYPE;

typedef struct _avindex
{
	uint32_t  dwAVC;    //00db 00dc 00dw
	uint32_t  dwParam;  //附加参数
	uint32_t  dwOffSet; //偏移量
	uint32_t  dwSize;   //大小
}AVINDEX;

typedef enum eAviFileStatus
{
	eFileStatusNull = 0,
	eFileStatusOpen = 1,
	eFileStatusWrite = 2
}EAVIFILESTATUS;


typedef enum eAviStreamStatus{
	eStreamStatusNull = 0,
	eStreamStatusInfo = 1,
	eStreamStatusFormat = 2,
	eStreamStatusWrite = 3,
}EAVISTREAMSTATUS;

typedef  struct AudioFormat{
	uint32_t    cbFormat;
	char      pbFormat[AUDIOFORMAT_LEN];
}AUDIOFORMAT;
#define AVI_MOVBOX_HD_SZ 8
#endif